package com.sailorj.controller;

import com.sailorj.entity.TbRepair;
import com.sailorj.service.TbRepairService;
import com.sailorj.util.PageBean;
import com.sailorj.util.SysLoging;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
* @Description: 维修管理
* @Param:
* @return:
* @Author: huangshengjun
* @Date: 2020/4/19 19:29
*/
@CrossOrigin  //后端解决跨域方法注解
@Controller
@RequestMapping("/repair") //访问的路径
public class TbRepairController {

    @Autowired
    private TbRepairService tbRepairService;

    @ResponseBody
    @SysLoging("添加维修信息") //根据SysLoging的注解信息，进行方法的注释
    @RequestMapping(value = "/addRepair",method = RequestMethod.POST) //restful风格
    public String addRepair(TbRepair tbRepair){
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        tbRepair.setCreatedTime(formatter.format(date));
        tbRepair.setIsDelete(0);
        tbRepairService.addRepair(tbRepair);
        return "";
    }

    @ResponseBody
    @SysLoging("根据id查询维修信息")
    @RequestMapping(value = "/getRepairById",method = RequestMethod.GET)
    public TbRepair getRepairById(int id){
        return tbRepairService.getRepairbById(id);
    }

    @ResponseBody
    @SysLoging("获取维修信息列表")
    @RequestMapping(value = "/getRepairList",method = RequestMethod.GET)
    PageBean getRepairList(@RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer size){
        return tbRepairService.getRepairList(page,size);
    }

    @ResponseBody
    @SysLoging("删除维修信息")
    @RequestMapping(value = "/deleteRepair",method = RequestMethod.DELETE)
    public String deleteRepair(TbRepair tbRepair) {
        TbRepair Repair = tbRepairService.getRepairbById(tbRepair.getId());
        Repair.setIsDelete(1);
        tbRepairService.updateRepairById(Repair);
        return "";
    }

    @ResponseBody
    @SysLoging("更新维修信息")
    @RequestMapping(value = "/updateRepair",method = RequestMethod.PUT)
    public String updateRepair(TbRepair tbRepair) {
        TbRepair Repair = tbRepairService.getRepairbById(tbRepair.getId());
        tbRepair.setCreatedTime(Repair.getCreatedTime());
        tbRepair.setIsDelete(0);
        tbRepairService.updateRepairById(tbRepair);
        return "";
    }

    @ResponseBody
    @SysLoging("导出维修信息")
    @RequestMapping(value = "/excelList",method = RequestMethod.GET)
    public void excelList(HttpServletResponse response) {
        XSSFWorkbook wb =tbRepairService.excelList();
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String fileName = formatter.format(date)+ "维修报表.xlsx";
        OutputStream outputStream =null;
        try {
            fileName = URLEncoder.encode(fileName,"UTF-8");
            //设置ContentType请求信息格式
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName);
            outputStream = response.getOutputStream();
            wb.write(outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
